package com.jie.langlangmall.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.StrUtil;
import com.github.pagehelper.PageHelper;
import com.jie.langlangmall.common.exception.ApiException;
import com.jie.langlangmall.dao.SmsCouponDao;
import com.jie.langlangmall.dao.SmsCouponProductCategoryRelationDao;
import com.jie.langlangmall.dao.SmsCouponProductRelationDao;
import com.jie.langlangmall.dto.SmsCouponParam;
import com.jie.langlangmall.mapper.SmsCouponMapper;
import com.jie.langlangmall.mapper.SmsCouponProductCategoryRelationMapper;
import com.jie.langlangmall.mapper.SmsCouponProductRelationMapper;
import com.jie.langlangmall.model.*;
import com.jie.langlangmall.service.SmsCouponService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @Author Jie
 * @Date 2024/11/26
 * @Email 3217998214@qq.com
 * @Description 优惠券
 */
@Service
public class SmsCouponServiceImpl implements SmsCouponService {

    @Autowired
    private SmsCouponMapper couponMapper;
    @Autowired
    private SmsCouponProductRelationMapper productRelationMapper;
    @Autowired
    private SmsCouponProductCategoryRelationMapper productCategoryRelationMapper;
    @Autowired
    private SmsCouponProductRelationDao productRelationDao;
    @Autowired
    private SmsCouponProductCategoryRelationDao productCategoryRelationDao;
    @Autowired
    private SmsCouponDao couponDao;

    /**
     * 添加优惠券
     *
     * @param couponParam
     */
    @Override
    @Transactional
    public void create(SmsCouponParam couponParam) {
        couponParam.setCount(couponParam.getPublishCount());
        couponParam.setUseCount(0);
        couponParam.setReceiveCount(0);
        // 插入表
        couponMapper.insert(couponParam);
        // 如果是指定商品的优惠券，插入和商品的关系表
        if (couponParam.getUseType().equals(2)) {
            if (CollectionUtil.isEmpty(couponParam.getProductRelationList())) {
                throw new ApiException("需要添加指定商品");
            }
            for (SmsCouponProductRelation productRelation : couponParam.getProductRelationList()) {
                productRelation.setCouponId(couponParam.getId());
            }
            productRelationDao.insertList(couponParam.getProductRelationList());
        }
        // 如果优惠券指定某类商品，插入和商品分类的关系表
        if (couponParam.getUseType().equals(1)) {
            if (CollectionUtil.isEmpty(couponParam.getProductCategoryRelationList())) {
                throw new ApiException("需要添加指定分类");
            }
            for (SmsCouponProductCategoryRelation couponProductCategoryRelation : couponParam.getProductCategoryRelationList()) {
                couponProductCategoryRelation.setCouponId(couponParam.getId());
            }
            productCategoryRelationDao.insertList(couponParam.getProductCategoryRelationList());
        }
    }

    /**
     * 删除优惠券
     *
     * @param id
     */
    @Override
    public void delete(Long id) {
        //删除优惠券
        couponMapper.deleteByPrimaryKey(id);
        //删除商品关联
        deleteProductRelation(id);
        //删除商品分类关联
        deleteProductCategoryRelation(id);
    }

    /**
     * 修改优惠券信息
     *
     * @param id
     * @param couponParam
     */
    @Override
    public void update(Long id, SmsCouponParam couponParam) {
        couponParam.setId(id);
        couponMapper.updateByPrimaryKey(couponParam);
        // 删除后插入优惠券和商品关系
        if (couponParam.getUseType().equals(2)) { // 指定某个商品
            for (SmsCouponProductRelation productRelation : couponParam.getProductRelationList()) {
                productRelation.setCouponId(couponParam.getId());
            }
            deleteProductRelation(id);
            productRelationDao.insertList(couponParam.getProductRelationList());
        }

        // 删除后插入优惠券和商品分类关系表
        if (couponParam.getUseType().equals(1)) {
            for (SmsCouponProductCategoryRelation couponProductCategoryRelation : couponParam.getProductCategoryRelationList()) {
                couponProductCategoryRelation.setCouponId(couponParam.getId());
            }
            deleteProductCategoryRelation(id);
            productCategoryRelationDao.insertList(couponParam.getProductCategoryRelationList());
        }

        // TODO: 如果之前是针对某个商品的优惠券，改成某类商品的优惠卷，需要清除之前的信息？
    }

    @Override
    public List<SmsCoupon> list(String name, Integer type, Integer pageSize, Integer pageNum) {
        SmsCouponExample example = new SmsCouponExample();
        SmsCouponExample.Criteria criteria = example.createCriteria();
        if (!StrUtil.isEmpty(name)) {
            criteria.andNameLike("%" + name + "%");
        }
        if (type != null) {
            criteria.andTypeEqualTo(type);
        }
        PageHelper.startPage(pageNum, pageSize);
        return couponMapper.selectByExample(example);
    }

    @Override
    public SmsCouponParam getItem(Long id) {
        return couponDao.getItem(id);
    }

    /**
     * 删除产品关联
     *
     * @param id
     */
    private void deleteProductRelation(Long id) {
        SmsCouponProductRelationExample productRelationExample = new SmsCouponProductRelationExample();
        productRelationExample.createCriteria().andCouponIdEqualTo(id);
        productRelationMapper.deleteByExample(productRelationExample);
    }

    /**
     * 删除分类关联
     *
     * @param id
     */
    private void deleteProductCategoryRelation(Long id) {
        SmsCouponProductCategoryRelationExample productCategoryRelationExample = new SmsCouponProductCategoryRelationExample();
        productCategoryRelationExample.createCriteria().andCouponIdEqualTo(id);
        productCategoryRelationMapper.deleteByExample(productCategoryRelationExample);
    }

}
